<template>
    <div class="wrapper">
        <el-row>
            <el-col :span="24">
                <common-card>
                    <template #header>
                        <div style="font-family: Roboto-Thin; padding: 20px; color: #fff">
                                <span style="font-size: 20px; display: inline-block; width: 100%">已发布消息列表
                                <el-button
                                        style="margin-left: 20px;"
                                        @click="addNew()"
                                        icon="el-icon-plus"
                                        type="primary"
                                        plain
                                        circle
                                        size="small"
                                        v-if="isTeacher"
                                />
                                </span>


                            <span style="font-size: 14px; display: inline-block; width: 100%; margin-top: 10px">消息列表</span>

                        </div>

                    </template>

                    <template #main>

                        <el-table
                                v-loading="tableLoading"
                                :max-height="height"
                                :data="tableData"
                                stripe
                                style="width: 100%">

                            <el-table-column
                                    prop="groupname"
                                    label="小组名称">
                            </el-table-column>

                            <el-table-column
                                    prop="content"
                                    label="小组信息内容">
                            </el-table-column>

                            <el-table-column
                                    label="操作"
                                    width="180"
                                    v-if="isTeacher">
                                <template slot-scope="scope">
                                    <el-button
                                            @click="editNew(scope.row)"
                                            icon="el-icon-setting"
                                            type="primary"
                                            plain
                                            circle
                                            size="small">
                                    </el-button>
                                    <el-button
                                            @click="deleteNew(scope.row)"
                                            icon="el-icon-delete"
                                            type="danger"
                                            plain
                                            circle
                                            size="small">
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </common-card>
            </el-col>
        </el-row>
        <add-dialog :dialog-visible="isAdd" :is-edit="isEdit" @closeDialog="isOpenDialog(false)"
                    :news-data="newsData" @fresh="fetchNewsByTeacher"/>
    </div>
</template>

<script>
    import CommonCard from '../../components/Cards/CommonCard';
    import addDialog from './addDialog'
    import * as scoreManage from '@/api/scoreManage';
    import * as newsManage from '@/api/newsManage';
    import Cookie from "js-cookie";


    export default {
        name: 'index',
        components: {
            CommonCard,
            addDialog
        },
        data() {
            return {
                total: 0,
                currentPage: 1,
                departmentData: {},
				subjectData: {},
                tableLoading: true,
                height: '',
                isAdd: false,
                isEdit: false,
                isTeacher: false,
                tableData: [],
                userRes: {},
                teacherID: '',
                teacherusername: '',
                newsData: {},
                codeUrl: '111111',
                reportUrl: '1111',
                commitData: {
                    studentid: '',
                    teacherid: '',
                    subjectid: ''
                },
                gridData: []
            };

        },
        methods: {
            async fetchNewsByTeacher() {
                console.log(this.teacherid);
                try {
                    let resInfo = JSON.parse(Cookie.get("userInfo"));
                    console.log(resInfo);
                    if (resInfo.role === 2){
                        this.studentid = resInfo.token;
                    }else if (resInfo.role === 1){
                        console.log('teacherOK');
                        this.teacherusername = resInfo.token + "";
                        console.log(resInfo.token);
                        console.log(this.teacherusername);
                        const userRes = await scoreManage.getUserId(this.teacherusername);
                        this.teacherID = userRes.msg.userid;
                        Cookie.set("teacherid",JSON.stringify(this.teacherID));
                        console.log("CESHI"+this.teacherID);
                    }
                    this.tableLoading = true;
                    if (this.teacherID){
                        this.isTeacher = true;
                    }
                    const res = await newsManage.selectByCondition({teacherid:this.teacherID});
                    console.log(res.msg);
                    this.tableData = res.msg;
                } catch (e) {
                    console.log(e)
                } finally {
                    this.tableLoading = false;
                }
            },
            isOpenDialog(isOpen, isEdit) {
                this.isAdd = isOpen;
                this.isEdit = isEdit;
            },

            async getTeacherInfo(){
                let resInfo = JSON.parse(Cookie.get("userInfo"));
                console.log(resInfo);
                if (resInfo.role === 2){
                    this.studentid = resInfo.token;
                }else if (resInfo.role === 1){
                    console.log('teacherOK');
                    this.teacherusername = resInfo.token + "";
                    console.log(resInfo.token);
                    console.log(this.teacherusername);
                    const userRes = await scoreManage.getUserId(this.teacherusername);
                    this.teacherID = userRes.msg.userid;
                    console.log(this.teacherID);
                }
            },
            
            
            async addNew() {
                try {
                    let resInfo = JSON.parse(Cookie.get("userInfo"));
                    console.log(resInfo);
                    if (resInfo.role === 2){
                        this.studentid = resInfo.token;
                    }else if (resInfo.role === 1){
                        console.log('teacherOK');
                        this.teacherusername = resInfo.token + "";
                        console.log(resInfo.token);
                        console.log(this.teacherusername);
                        const userRes = await scoreManage.getUserId(this.teacherusername);
                        this.teacherID = userRes.msg.userid;
                        console.log("CESHI"+this.teacherID);
                    }
                    this.tableLoading = true;
                    this.newsData.teacherid = this.teacherID
                    this.isOpenDialog(true, false);
                } catch (e) {
                    console.log(e);
                }
            },
            async editNew(data) {
                try {
                    this.newsData = data;
                    this.isOpenDialog(true, true);
                } catch (e) {
                    console.log(e);
                }
            },
            async deleteNew(data){
                console.log(data);
                try {
                    await this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    });
                    await newsManage.deleteNews(data.newsid);
                    this.$notify({
                        title: '信息',
                        type: 'success',
                        message: '删除成功!'
                    });
                    await this.fetchNewsByTeacher();
                } catch (e) {
                    console.log(e)
                }
            },
            handleClose() {
                this.$emit('closeDialog');
            },
            handleFresh() {
                this.$emit('fresh');
            },
            copyClick(row, column, cell, event) {
                this.$refs.popover4.showPopper = false;
                // document.body.click()
            }
        },
        created () {
            this.height = window.innerHeight - 150;
            this.getTeacherInfo();
            this.fetchNewsByTeacher();
        }
        }
</script>

<style scoped>
    .wrapper {
        padding: 0 15px;
    }

    .tableSearch {
        width: 221px;
    }

    .tableSearch >>> .el-input__inner {
        border-radius: 415px;
    }

    .btn {
        margin-right: 7px;
    }
</style>